package com.chengma.service.impl;

import java.util.*;

import com.chengma.common.PageResult;
import com.chengma.domain.TbComment;
import com.chengma.domain.TbQuestion;
import com.chengma.domain.TbUserCollectedQuestion;
import com.chengma.domain.vo.CommentTreeDTO;
import com.chengma.mapper.TbCommentMapper;
import com.chengma.mapper.TbUserCollectedCommentMapper;
import com.chengma.mapper.TbUserCollectedQuestionMapper;
import com.chengma.mapper.TbUserLikedCommentMapper;
import com.chengma.service.ITbCommentService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 评论Service业务层处理
 *
 * @author tck
 * @date 2025-03-19
 */
@Service
public class TbCommentServiceImpl implements ITbCommentService {
    @Autowired
    private TbCommentMapper tbCommentMapper;
    @Autowired
    private TbUserLikedCommentMapper tbUserLikedCommentMapper;
    @Autowired
    private TbUserCollectedCommentMapper tbUserCollectedCommentMapper;

    /**
     * 查询评论
     *
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public TbComment selectTbCommentById(Long id) {
        return tbCommentMapper.selectTbCommentById(id);
    }

    /**
     * 查询评论列表
     *
     * @param tbComment 评论
     * @return 评论
     */
    @Override
    public List<TbComment> selectTbCommentList(TbComment tbComment) {
        return tbCommentMapper.selectTbCommentList(tbComment);
    }

    /**
     * 新增评论
     *
     * @param tbComment 评论
     * @return 结果
     */
    @Override
    public int insertTbComment(TbComment tbComment) {
        tbComment.setCreateTime(new Date());
        return tbCommentMapper.insertTbComment(tbComment);
    }

    /**
     * 修改评论
     *
     * @param tbComment 评论
     * @return 结果
     */
    @Override
    public int updateTbComment(TbComment tbComment) {
        return tbCommentMapper.updateTbComment(tbComment);
    }

    /**
     * 批量删除评论
     *
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteTbCommentByIds(Long[] ids) {
        return tbCommentMapper.deleteTbCommentByIds(ids);
    }

    /**
     * 删除评论信息
     *
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteTbCommentById(Long id) {
        return tbCommentMapper.deleteTbCommentById(id);
    }

    public List<CommentTreeDTO> getCommentTree(String questionId) {
        List<CommentTreeDTO> flatComments = tbCommentMapper.getCommentsByQuestionId(questionId);
        for (CommentTreeDTO commentTreeDTO : flatComments) {
            Integer commentId = commentTreeDTO.getId();//评论id
            //用于汇总评论点赞数量
            commentTreeDTO.setLikedNum(tbUserLikedCommentMapper.getCommentLikes(commentId));
            //用于汇总评论收藏数量
            commentTreeDTO.setCollectedNum(tbUserCollectedCommentMapper.getCommentCollected(Integer.valueOf(commentId)));
            //用于汇总评论的评论数量
            commentTreeDTO.setCommentNum(tbCommentMapper.getCommentLikesNUm(Integer.valueOf(commentId)));
        }
        return buildTree(flatComments);
    }

    private List<CommentTreeDTO> buildTree(List<CommentTreeDTO> comments) {
        Map<Integer, CommentTreeDTO> commentMap = new HashMap<>();
        List<CommentTreeDTO> rootComments = new ArrayList<>();

        // 第一遍遍历：存入Map
        for (CommentTreeDTO comment : comments) {
            comment.setChildren(new ArrayList<>());
            commentMap.put(comment.getId(), comment);
        }

        // 第二遍遍历：构建树
        for (CommentTreeDTO comment : comments) {
            if (comment.getReplyId() == 0) {
                rootComments.add(comment);
            } else {
                CommentTreeDTO parent = commentMap.get(comment.getReplyId());
                if (parent != null) {
                    parent.getChildren().add(comment);
                }
            }
        }
        return rootComments;
    }

    // Service 层新增方法
    public PageResult<CommentTreeDTO> getComments(String questionId, int page, int size) {
        PageHelper.startPage(page, size);
        List<CommentTreeDTO> comments = tbCommentMapper.getCommentsByQuestionId(questionId);
        return new PageResult<>(comments);
    }


}
